package com.yun.apply.system.service.impl;

import com.yun.apply.system.mapper.SysDictDataMapper;
import com.yun.apply.system.mapper.SysDictTypeMapper;
import com.yun.apply.system.service.ISysDictTypeService;
import com.yun.common.core.domain.SysDictData;
import com.yun.common.core.domain.SysDictType;
import com.yun.common.exception.CustomException;
import com.yun.common.utils.DictUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.PostConstruct;
import java.util.Date;
import java.util.List;

@Service
public class SysDictTypeServiceImpl implements ISysDictTypeService {

    @Autowired
    private SysDictTypeMapper dictTypeMapper;
    @Autowired
    private SysDictDataMapper dictDataMapper;

    /**
     * 项目启动时，初始化字典到缓存
     */
    @PostConstruct
    public void init()
    {
        List<SysDictType> dictTypeList = dictTypeMapper.selectList(null);
        for (SysDictType dictType : dictTypeList)
        {
            List<SysDictData> dictDatas = dictDataMapper.selectDictDataByType(dictType.getDictType());
            DictUtils.setDictCache(dictType.getDictType(), dictDatas);
        }
    }

    /**
     * 根据条件分页查询字典类型
     *
     * @param dictType 字典类型信息
     * @return 字典类型集合信息
     */
    @Override
    public List<SysDictType> selectDictTypeList(SysDictType dictType) {
        return dictTypeMapper.selectDictTypeList(dictType);
    }

    @Transactional
    @Override
    public int insertDictType(SysDictType dict) {
        this.checkDictType(dict);
        dict.setCreateTime(new Date());
        int row =  dictTypeMapper.insert(dict);
        if (row > 0)
        {
            DictUtils.clearDictCache();
        }
        return row;
    }

    @Transactional
    @Override
    public int updateDictType(SysDictType dict) {
        this.checkDictType(dict);
        dict.setUpdateTime(new Date());
        int row = dictTypeMapper.updateById(dict);
        if (row > 0) {
            DictUtils.clearDictCache();
        }
        return row;
    }

    @Override
    public SysDictType selectDictTypeById(Long dictId) {
        return dictTypeMapper.selectById(dictId);
    }

    @Transactional
    @Override
    public int deleteDictTypeByIds(Long[] dictIds) {
        for (Long dictId : dictIds)
        {
            SysDictType dictType = selectDictTypeById(dictId);
            if (dictDataMapper.countDictDataByType(dictType.getDictType()) > 0)
            {
                throw new CustomException(String.format("%1$s已分配,不能删除", dictType.getDictName()));
            }
        }
        int count = dictTypeMapper.deleteDictTypeByIds(dictIds);
        if (count > 0)
        {
            DictUtils.clearDictCache();
        }
        return count;
    }

    @Override
    public void clearCache() {
        DictUtils.clearDictCache();
    }

    @Override
    public List<SysDictData> selectDictDataByType(String dictType) {
        List<SysDictData> dictDatas = DictUtils.getDictCache(dictType);
        if (dictDatas != null && dictDatas.size() > 0)
        {
            return dictDatas;
        }
        dictDatas = dictDataMapper.selectDictDataByType(dictType);
        if (dictDatas != null && dictDatas.size() > 0)
        {
            DictUtils.setDictCache(dictType, dictDatas);
            return dictDatas;
        }
        return null;
    }

    @Override
    public List<SysDictType> selectDictTypeAll() {
        return dictTypeMapper.selectList(null);
    }

    private void checkDictType(SysDictType dict) {
        Long dictId = dict.getDictId() == null ? -1L : dict.getDictId();
        int result = dictTypeMapper.checkDictTypeUnique(dict.getDictType(), dictId);
        if (result > 0) {
            throw new CustomException(dict.getDictName() + "字典类型已存在");
        }
    }
}
